#include "GM/GmMgr.h"
#include "ProtoBuffer/ServerInternalCmd.pb.h"
#include "ProtoBuffer/ServerInternalProtocol.pb.h"
#include "LogicService.h"
#include "Common/TableData/GMCommandGMCommandCfg.h"
#include "LogicService.h"
#include "Character/PlayerMgr.h"
#include "Character/Player.h"
#include "Common/TableData/RoleExpCfg.h"
#include "Character/monster/MonsterRefCfg.h"
#include "Character/monster/MonsterMgr.h"
#include "Common/lexical_cast.hpp"
#include "Scene/MapMgr.h"
#include "Common/AttrMgr/AttrMgr.h"
#include "PlaneMgr/PlaneMgr.h"
#include "Effect/EffectMgr.h"
#include "Character/OperateLimitPart.h"

GmMgr::GmMgr()
{
}

GmMgr::~GmMgr()
{
}

bool GmMgr::Init()
{
	bool result = false;
	bool retCode = false;

	IMessageDispatch *pMessageDispatch = g_GetLogicService()->GetServiceMessageDispatch();
	if (nullptr == pMessageDispatch)
	{
		return false;
	}

	retCode=pMessageDispatch->registerClientHandler(EMODULE_ID_GM, this);
	MMOLOG_PROCESS_ERROR(retCode);
	retCode=pMessageDispatch->registerServerHandler(EMODULE_ID_GM, this);
	MMOLOG_PROCESS_ERROR(retCode);
	

	//后面根据关键字，慢慢加
	m_gmFunc["lvup"] = &GmMgr::OnCharacterLevelUp;
	m_gmFunc["goto"] = &GmMgr::OnCharacterGotoPos;
	m_gmFunc["mapto"] = &GmMgr::OnCharacterGotoMap;
	m_gmFunc["gold"] = &GmMgr::OnCharacterAddGold;
	m_gmFunc["diamond"] = &GmMgr::OnCharacterAddDiamond;
	m_gmFunc["attribute"] = &GmMgr::OnCharacterAddAttr;
	m_gmFunc["collect"] = &GmMgr::SetCollectSkill;
	m_gmFunc["make"] = &GmMgr::SetMakeSkill;
	m_gmFunc["addcollect"] = &GmMgr::AddCollectEnergy;
	m_gmFunc["addmake"] = &GmMgr::AddMakeEnergy;
	m_gmFunc["addstatebag"] = &GmMgr::AddStateBag;
	m_gmFunc["monster"] = &GmMgr::AddMonster;
	m_gmFunc["ai_on"] = &GmMgr::AIOn;
	m_gmFunc["ai_off"] = &GmMgr::AIOff;
	m_gmFunc["suicide"] = &GmMgr::Suicide;
	m_gmFunc["invincible"] = &GmMgr::Invincible;
	m_gmFunc["get"] = &GmMgr::GetItem;
	//设置时间注释掉，避免测试调时间引起数据错误
	m_gmFunc["settime"] = &GmMgr::SetTime;
	m_gmFunc["startplane"] = &GmMgr::StartPlane;
	m_gmFunc["stopplane"] = &GmMgr::StopPlane;
	m_gmFunc["task_receive"] = &GmMgr::ReceiveMission;
	m_gmFunc["task_finish"] = &GmMgr::FinishMission;
	m_gmFunc["task_end"] = &GmMgr::EndMission;
	m_gmFunc["task_sigle_finish"] = &GmMgr::SigleFinishMission;
	m_gmFunc["task_sigle_remove"] = &GmMgr::RemoveMission;	
	m_gmFunc["trigger"] = &GmMgr::OnTrigger;
	m_gmFunc["useskill"] = &GmMgr::OnUseSkill;
	m_gmFunc["mapgoto"] = &GmMgr::OnMaoGoTo;
	m_gmFunc["trs_areaid"] = &GmMgr::SetTreasureArea;
	m_gmFunc["pinkdiamond"] = &GmMgr::OnGetBindDiamond;
	m_gmFunc["add_PK"] = &GmMgr::OnAddPKValue;
	m_gmFunc["clear_package"] = &GmMgr::ClearPackage;
	m_gmFunc["sethuoyue"] = &GmMgr::SetHuoYue;

	result = true;
Exit0:
	return result;
}

bool GmMgr::UnInit()
{
	bool result = false;
	bool retCode = false;

	IMessageDispatch *pMessageDispatch = g_GetLogicService()->GetServiceMessageDispatch();
	if (nullptr == pMessageDispatch)
	{
		return false;
	}
	retCode = pMessageDispatch->unregisterClientHandler(EMODULE_ID_GM);
	MMOLOG_PROCESS_ERROR(retCode);

	retCode = pMessageDispatch->unregisterServerHandler(EMODULE_ID_GM);
	MMOLOG_PROCESS_ERROR(retCode);

	result = true;
Exit0:
	return result;
}

//客户端直接来的消息，可能会有，现在没用到
void GmMgr::onClientMessage(uint32_t actionId, RECV_PACKAGE &package, uint64_t Id)
{
	if (actionId != CLIENT_TO_LOGIC_GM)
		return;

	LogicGM gm;
	if (!gm.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
		return;
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(Id);
	if (nullptr != pPlayer && ACCOUNT_GAME_STATE_SWITCH == pPlayer->GetAccountState())
	{
		LogErrFmtPrint("GmMgr::onClientMessage...ACCOUNT_GAME_STATE_SWITCH == pPlayer->GetAccountState()...cid:%lu", Id);
	}
	OnHangleMsg(Id,gm);
}
//从中心服转发过来的GM消息
void GmMgr::onServerMessage(uint32_t actionId, RECV_PACKAGE &package)
{
	if (actionId != CENTER_TO_LOGIC_GM && actionId != CENTER_TO_LOGIC_WEBGM)
		return;

	LogicGM gm;
	if (!gm.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
		return;

	OnHangleMsg(gm.cid(), gm, actionId == CENTER_TO_LOGIC_WEBGM);
}

bool GmMgr::OnHangleMsg(CharIDType Id, LogicGM & gm, bool fromWeb /* = false */)
{
	string cmd;
	int32_t cmdID = 0;
	uint32_t param = 0;
	CharIDType cid = 0;
	ProtoBuf::GMResult msgResult;
	vector<std::string > vArgs;

	//如果是网页那边发来的则带上网页Id
	if (gm.has_webid())
	{
		msgResult.set_webid(gm.webid());
	}
	
	if (gm.cid() == 0)
	{
		cid = Id;
	}
	else
		cid = gm.cid();

	cmd = gm.gmcmd();

	cmdID = CommonApi::StrToInt(cmd);
	if (cmdID)
	{
		const GMCommandGMCommandCfgInfo *pConfig = g_GetGMCommandGMCommandCfgTable()->GetGMCommandGMCommandCfgInfo(cmdID);
		if (pConfig)
		{
			cmd = pConfig->Key;
			param = pConfig->ParamCount;
		}
	}
	else
	{
		const GMCommandGMCommandCfgMap * pMap = g_GetGMCommandGMCommandCfgTable()->GetGMCommandGMCommandCfgMap();
		for (GMCommandGMCommandCfgMap::const_iterator iter = pMap->begin(); iter != pMap->end(); ++iter)
		{
			if (strcmp(iter->second.Key.c_str(), cmd.c_str()) == 0)
			{
				param = iter->second.ParamCount;
			}
		}
	}

	if (cmd.empty())
	{
		msgResult.set_retcode(RET_GM_NOT_KEY);
		LogDebugFmtPrint("gmcommand is not key");
		if (cid>0)
		{
			if (fromWeb)
			{
				g_GetLogicService()->SendDataToCenter(EMODULE_ID_RECV_ZONE_MSG, SERVER_GM_RESULT, &msgResult); 
			}
			else
			{
				g_GetLogicService()->SendDataToClient(cid, EMODULE_ID_GM, SERVER_TO_CLIENT_GM_RESULT, &msgResult);
			}
		}
		return false;
	}

	if (param>0 &&(uint32_t)gm.agrs_size() != param)
	{
		msgResult.set_retcode(RET_GM_PRAMA_COUNT_ERROR);
		LogDebugFmtPrint("gmcommand is not param count");
		if (cid>0)
		{
			if (fromWeb)
			{
				g_GetLogicService()->SendDataToCenter(EMODULE_ID_RECV_ZONE_MSG, SERVER_GM_RESULT, &msgResult); 
			}
			else
			{
				g_GetLogicService()->SendDataToClient(cid, EMODULE_ID_GM, SERVER_TO_CLIENT_GM_RESULT, &msgResult);
			}

		}
		
		return false;
	}

	for (int i = 0; i < gm.agrs_size(); i++)
		vArgs.push_back(gm.agrs(i));

	GmFunc::iterator iter = m_gmFunc.find(cmd);
	GmFunc::iterator iterEnd = m_gmFunc.end();

	if (iter != iterEnd)
		(this->*iter->second)(cid, vArgs);
	else
	{
		msgResult.set_retcode(RET_GM_NOT_FIND);
		if (cid>0)
		{
			if (fromWeb)
			{
				g_GetLogicService()->SendDataToCenter(EMODULE_ID_RECV_ZONE_MSG, SERVER_GM_RESULT, &msgResult); 
			}
			else
			{
				g_GetLogicService()->SendDataToClient(cid, EMODULE_ID_GM, SERVER_TO_CLIENT_GM_RESULT, &msgResult);
			}
		}
		
		return false;
	}

	msgResult.set_retcode(RET_SUCCESS);
	if (cid>0)
	{
		if (fromWeb)
		{
			g_GetLogicService()->SendDataToCenter(EMODULE_ID_RECV_ZONE_MSG, SERVER_GM_RESULT, &msgResult);
		}
		else
		{
		  g_GetLogicService()->SendDataToClient(cid, EMODULE_ID_GM, SERVER_TO_CLIENT_GM_RESULT, &msgResult);
		}
	}

	if (fromWeb)
	{
		LogInfoFmtPrint("logic recv Gm cmd from web, cmd:%s", cmd.c_str());
	}
	else
	{
		LogInfoFmtPrint("logic recv Gm cmd from client, cmd:%s", cmd.c_str());
	}

	return true;
}


bool GmMgr::OnCharacterLevelUp(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		
		//找到该等级所需要的经验值
		const RoleExpCfgInfo *pcfg = g_GetRoleExpCfgTable()->GetRoleExpCfgInfo(CommonApi::StrToInt(args[0]));
		if (pcfg)
		{

			uint32_t nLevel = CommonApi::StrToInt(args[0]);

			if (nLevel>60)
			{
				return false;
			}

			if ((uint32_t)pPlayer->GetAttr(C_LEVEL) > nLevel)
			{
			/*	pPlayer->SetAttr(C_EXP, 0);
				pPlayer->SetAttr(C_EXPMAX, pcfg->exp);
				pPlayer->SetAttr(C_LEVEL, CommonApi::StrToInt(args[0]), true);*/
			}
			else
			{
				for (uint32_t i = (uint32_t)pPlayer->GetAttr(C_LEVEL); i < nLevel;++i)
				{
					const RoleExpCfgInfo *pcfg = g_GetRoleExpCfgTable()->GetRoleExpCfgInfo(i);
					pPlayer->AddExp(pcfg->exp);
				}
			}

			pPlayer->SetAttr(C_EXP, 0, true);
		}
	//	pPlayer->SetAttr(C_HP, pPlayer->GetAttr(C_MAX_HP));
		//pPlayer->SetAttr(C_MP, pPlayer->GetAttr(C_MAX_MP),true);
	}
	return true;
}

bool GmMgr::OnCharacterGotoPos(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		MovePart * pPart =dynamic_cast<MovePart*> (pPlayer->GetPart(PART_MOVE));
		if (pPart)
		{
			Point3<float> despos;
			despos.x =  CommonApi::StrToFloat(args[0]);
			despos.y =  CommonApi::StrToFloat(args[1]);
			despos.z = CommonApi::StrToFloat(args[2]);
			pPart->Teleporting(despos);
		}
	}

	return true;
}

bool GmMgr::OnCharacterGotoMap(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		Point3<float> pos = g_GetMapLocationCfg()->GetFirstTeleportPos(CommonApi::StrToInt(args[0]));

		 MovePart * pPart = dynamic_cast<MovePart *>(pPlayer->GetPart(PART_MOVE));
		 if (pPart)
		 {
			 //切换场景的时候打印下日志，便于后面查找定位问题
			 LogDebugFmtPrint("[logic] GmMgr::OnCharacterGotoMap...cid:%lu, sceneid:%u, mapid:%u, pos:%f,%f,%f   ",cid, CommonApi::StrToInt(args[0]), CommonApi::StrToInt(args[0]), pos.x, pos.y, pos.z);
			 STransParam transParam;
			 transParam.transType = ETransType_GM;
			 pPart->TransScene(CommonApi::StrToInt(args[0]), pos, CommonApi::StrToInt(args[0]), transParam);
		 }
	}

	return true;
}

bool GmMgr::OnMaoGoTo(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
	
		MovePart * pPart = dynamic_cast<MovePart *>(pPlayer->GetPart(PART_MOVE));
		if (pPart)
		{
			Point3<float> pos;
			pos.x = CommonApi::StrToInt(args[1]);
			pos.y = CommonApi::StrToInt(args[2]);
			pos.z = CommonApi::StrToInt(args[3]);

			//切换场景的时候打印下日志，便于后面查找定位问题
			LogDebugFmtPrint("[logic] GmMgr::OnMaoGoTo...cid:%lu, sceneid:%u, mapid:%u, pos:%f,%f,%f   ",cid, CommonApi::StrToInt(args[0]), CommonApi::StrToInt(args[0]), pos.x, pos.y, pos.z);
			STransParam transParam;
			transParam.transType = ETransType_GM;
			pPart->TransScene(CommonApi::StrToInt(args[0]), pos, CommonApi::StrToInt(args[0]), transParam);
		}
	}

	return true;
}

bool GmMgr::SetTreasureArea(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{

		PackagePart * pPart = dynamic_cast<PackagePart *>(pPlayer->GetPart(PART_PACKAGE));
		if (pPart)
		{
			return pPart->SetTreasureArea(CommonApi::StrToInt(args[0]), CommonApi::StrToInt(args[1]));
		}
	}

	return true;
}

bool GmMgr::OnCharacterAddGold(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		//pPlayer->AddAttr(C_GOLD, CommonApi::StrToInt(args[0]), true);
		pPlayer->AddSourceAttr(C_GOLD, CommonApi::StrToInt(args[0]), true, S_GM);
	}

	return true;
}

bool GmMgr::OnCharacterAddDiamond(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		//pPlayer->AddAttr(C_DIAMOND, CommonApi::StrToInt(args[0]), true);
		pPlayer->AddSourceAttr(C_DIAMOND, CommonApi::StrToInt(args[0]), true, S_GM);
	}

	return true;
}
bool GmMgr::OnGetBindDiamond(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		//pPlayer->AddAttr(C_DIAMOND_BIND, CommonApi::StrToInt(args[0]), true);
		pPlayer->AddSourceAttr(C_DIAMOND_BIND, CommonApi::StrToInt(args[0]), true, S_GM);
	}

	return true;
}

bool GmMgr::OnCharacterAddAttr(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		//这里如果不用相加的，则直接加到属性上
		//pPlayer->AddAttrCache(CommonApi::StrToInt(args[0]), CommonApi::StrToInt(args[1]));
		if (CommonApi::StrToInt(args[0]) < P_FIGHT_ATTR_END)   //这里加战斗属性
		{
			pPlayer->AddAttr(g_GetAttrMgr()->GetLevCNumAttr(CREATURE_PLAYER, CommonApi::StrToInt(args[0])), CommonApi::StrToInt(args[1]), true);
		}
		else
		{
			//非战斗属性，有些不能通过属性添加的去掉,后续发现添加其它的
			if (CommonApi::StrToInt(args[0])!=P_LEVEL)
			{
				pPlayer->AddAttrCache(CommonApi::StrToInt(args[0]), CommonApi::StrToInt(args[1]));
			}
		}
	}

	return true;
}

bool GmMgr::SetCollectSkill(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		DeputyPart *pPart = dynamic_cast<DeputyPart*>(pPlayer->GetPart(PART_DEPUTY));
		if (pPart)
		{
			pPart->SetCollectSkill(CommonApi::StrToInt(args[0]));
		}
	}
	return true;
}

bool GmMgr::SetMakeSkill(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		DeputyPart *pPart = dynamic_cast<DeputyPart*>(pPlayer->GetPart(PART_DEPUTY));
		if (pPart)
		{
			pPart->SetMakeSkill(CommonApi::StrToInt(args[0]));
		}
	}
	return true;
}

bool GmMgr::AddCollectEnergy(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		DeputyPart *pPart = dynamic_cast<DeputyPart*>(pPlayer->GetPart(PART_DEPUTY));
		if (pPart)
		{
			pPart->AddCollectEnergy(CommonApi::StrToInt(args[0]), CommonApi::StrToInt(args[1]));
		}
	}
	return true;
}

bool GmMgr::AddMakeEnergy(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		DeputyPart *pPart = dynamic_cast<DeputyPart*>(pPlayer->GetPart(PART_DEPUTY));
		if (pPart)
		{
			pPart->AddMakeEnergy(CommonApi::StrToInt(args[0]), CommonApi::StrToInt(args[1]));
		}
	}
	return true;
}

bool GmMgr::AddStateBag(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		SkillPart *pPart = dynamic_cast<SkillPart*>(pPlayer->GetPart(PART_SKILL));
		if (pPart)
		{
			pPart->AddStateBag(CommonApi::StrToInt(args[0]), CommonApi::StrToInt(args[1]));
		}
	}

	return true;
}

bool GmMgr::AddMonster(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		int monsterId = lexical_cast<int>(args[0]);
		int monsterNum = lexical_cast<int>(args[1]);

		Point3<float> pos = pPlayer->GetPos();

		auto pMap = g_GetMapMgr()->GetMap(pPlayer->GetMapId());
		if (pMap == nullptr)
		{
			return false;
		}

		int x = 1;
		int z = 1;
		//int error = 0; //无法再错误的地点放怪物
		for(int i = 0; i < monsterNum; i++)
		{
			if (x == 10)
			{
				x = 1;
			}
			x = i;
			Point3<float> newPos = pos;
			newPos.x = newPos.x + 2 * x;
			newPos.z = newPos.z + 2 * z;

			//if (pMap->FindNearestPos(pos.x, pos.z, pos.y, &newPos.x, &newPos.z, &newPos.y, nullptr))
			{
				Point3<float> curDir = Point3<float>(0,0,1);
				MonsterExtendInfo stInfo;
				stInfo.set_mastercid(pPlayer->GetCid());
				MonsCreateParam createParam;
				createParam.createType = EMonsCreateType_GM;
				Monster * pMonster = g_GetCreatureMgr()->CreateMonster((uint32_t)monsterId, pPlayer->GetSceneId(), newPos, createParam, curDir, -1, &stInfo, cid);
				if (pMonster == nullptr)
				{
					LogInfoFmtPrint("Create Monster Failed, MonsterId:%d Scene:%d", monsterId, pPlayer->GetSceneId());
					return false;
				}
				pMonster->SetMonsterMasterCID(cid);
				pMonster->SetMonsterTeamID(pPlayer->GetAttr(C_TEAM_ID));
			}
			//else
			//{
			//	//错误发生100次，可能找不到地点发怪物了， 防止死循环
			//	if (error >= 100)
			//	{
			//		LogInfoFmtPrint("Create Monster Failed, no place");
			//		break;
			//	}
			//	error++;
			//	x = 1;
			//	z += 1;
			//	i -= 1;
			//}
		}
	}
	return true;
}

bool GmMgr::AIOn(CharIDType cid, vector<std::string> &args)
{
	g_GetCreatureMgr()->SetAIEnable(true);
	return true;
}

bool GmMgr::AIOff(CharIDType cid, vector<std::string> &args)
{
	g_GetCreatureMgr()->SetAIEnable(false);
	return true;
}

bool GmMgr::Suicide(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		int32_t hp = pPlayer->GetAttr(C_HP);
		pPlayer->AddAttr(C_HP, -hp, true, 0);

		//DieEvent dieEvent;
		//dieEvent.killerCid = 0;
		//dieEvent.count = 1;
		//dieEvent.sceneId = pPlayer->GetSceneId();
		//dieEvent.nCid = pPlayer->GetCid();
		//g_GetEvent()->FireExecute(EVENT_CREATURE_DEAD, pPlayer->GetCid(), pPlayer->CreatureKind(), (DieEvent*)&dieEvent, sizeof(dieEvent));

		LogDebugFmtPrint("player  killmyself ondead, cid:%llu", pPlayer->GetCid());

	}
	return true;
}

bool GmMgr::Invincible(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		pPlayer->AddBuffState(CREATURE_BUFF_STATE_WUDI);
	}
	return true;
}

bool GmMgr::GetItem(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		uint32_t itemId = (uint32_t)lexical_cast<int>(args[0]);
		uint32_t itemNum = (uint32_t)lexical_cast<int>(args[1]);


		Part* pPart = pPlayer->GetPart(PART_PACKAGE);
		if (pPart == nullptr)
		{
			return false;
		}
		SItemSourceParam sourceParam;
		sourceParam.source = S_GM;
		return pPart->AddItem(itemId, itemNum, sourceParam);
	}
	return false;
}

bool GmMgr::SetTime(CharIDType cid, vector<std::string> &args)
{
	uint32_t year = CommonApi::StrToInt(args.at(0));;
	uint32_t month = CommonApi::StrToInt(args.at(1));
	uint32_t day = CommonApi::StrToInt(args.at(2));
	uint32_t hour = CommonApi::StrToInt(args.at(3));
	uint32_t minute = CommonApi::StrToInt(args.at(4));
	uint32_t sec = CommonApi::StrToInt(args.at(5));

	LogInfoFmtPrint("GmMgr::SetTime...year:%u,month:%u,day:%u,hour:%u,minite:%u,sec:%u ", year, month, day, hour, minute, sec);
	Time::GmSetLinuxTime(year, month, day, hour, minute, sec);
	return true;
}

bool GmMgr::StartPlane(CharIDType cid, vector<std::string> &args)
{
	std::vector<uint32_t> vConfigID;
	for (int i = 0; i < (int)args.size();++i)
	{
		vConfigID.push_back(CommonApi::StrToInt(args.at(i)));
	}
	if (vConfigID.size()>0)
	{
		g_GetPlaneMgr()->CreatePlane(cid, vConfigID[0]);
	}
	
	return true;
}

bool GmMgr::StopPlane(CharIDType cid, vector<std::string> &args)
{
	g_GetPlaneMgr()->DestroyPlane(cid,0);
	return true;
}

bool GmMgr::ReceiveMission(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		uint32_t missionId = (uint32_t)lexical_cast<int>(args[0]);


		MissionPart* pPart = dynamic_cast<MissionPart*>(pPlayer->GetPart(PART_MISSION));
		if (pPart == nullptr)
		{
			return false;
		}

		return pPart->CheatAcceptMission(missionId);
	}
	return false;
}

bool GmMgr::FinishMission(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		uint32_t beginMissionId = (uint32_t)lexical_cast<int>(args[0]);
		uint32_t endMissionId = (uint32_t)lexical_cast<int>(args[1]);

		MissionPart* pPart = dynamic_cast<MissionPart*>(pPlayer->GetPart(PART_MISSION));
		if (pPart == nullptr)
		{
			return false;
		}

		pPart->CheatFastFinishMission(beginMissionId, endMissionId);
		return true;
	}
	return false;
}

bool GmMgr::SigleFinishMission(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		uint32_t missionId = (uint32_t)lexical_cast<int>(args[0]);

		MissionPart* pPart = dynamic_cast<MissionPart*>(pPlayer->GetPart(PART_MISSION));
		if (pPart == nullptr)
		{
			return false;
		}

		pPart->CheatFastFinishSigleMission(missionId);
		return true;
	}
	return false;
}

bool GmMgr::EndMission(CharIDType cid, vector<std::string> &args)
{
	Player*  pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		uint32_t missionId = (uint32_t)lexical_cast<int>(args[0]);


		MissionPart* pPart = dynamic_cast<MissionPart*>(pPlayer->GetPart(PART_MISSION));
		if (pPart == nullptr)
		{
			return false;
		}

		return pPart->CheatSubmitMission(missionId);
	}
	return false;
}

bool GmMgr::OnTrigger(CharIDType cid, vector<std::string> &args)
{
	Creature * pCreature = g_GetCreatureMgr()->GetCreature(cid);
	if (pCreature)
	{
		g_GetEffectMgr()->OnEffect(pCreature,CommonApi::StrToInt(args[0]));
	}
	return true;
}

bool GmMgr::OnUseSkill(CharIDType cid, vector<std::string> &args)
{
	Player * pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		SkillPart * pPart = dynamic_cast<SkillPart*>(pPlayer->GetPart(PART_SKILL));
		if (pPart)
		{
			vecUint64_t tmpCid;
			pPart->BeginUseSkillByItem(CommonApi::StrToInt(args[0]),1);
		}
	}
	return true;
}

bool GmMgr::OnAddPKValue(CharIDType cid, vector<std::string> &args)
{
	Player * pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		PvpPart * pPvpPart = dynamic_cast<PvpPart*>(pPlayer->GetPart(PART_PVP));
		if (pPvpPart)
		{
			if (CommonApi::StrToInt(args[0])>0)
			{
				pPvpPart->AddPkKillingValue(CommonApi::StrToInt(args[0]));
			}
			else
			{
				pPvpPart->SubPkKillingValue(-1*CommonApi::StrToInt(args[0]));
			}
			
			pPvpPart->SendPvpKillingValue();
		}
	}

	return true;
}

bool GmMgr::ClearPackage(CharIDType cid, vector<std::string> &args)
{
	_UNUSE(args);
	Player * pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		Part *pPackage = pPlayer->GetPart(PART_PACKAGE);
		if (nullptr != pPackage)
		{
			return pPackage->ClearPackage();
		}
	}
	return false;
}

bool GmMgr::RemoveMission(CharIDType cid, vector<std::string> &args)
{
	_UNUSE(args);
	Player * pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		MissionPart *pMission = dynamic_cast<MissionPart*>(pPlayer->GetPart(PART_MISSION));
		if (nullptr != pMission)
		{
			if (CommonApi::StrToInt(args[0]) > 0)
			{
				pMission->CheatRemoveSingleMissiion(CommonApi::StrToInt(args[0]));
				return true;
			}			
		}
	}
	return false;
}

bool GmMgr::SetHuoYue(CharIDType cid, vector<std::string> &args)
{
	_UNUSE(args);
	Player * pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (pPlayer)
	{
		OperateLimitPart *pPart = dynamic_cast<OperateLimitPart*>(pPlayer->GetPart(PART_OPERATELIMIT));
		if (nullptr != pPart)
		{
			if (CommonApi::StrToInt(args[0]) > 0)
			{
				pPart->SetOprateLimit(PRSTE_HUOYUEDU, CommonApi::StrToInt(args[0]));
				return true;
			}
		}
	}
	return false;
}
